﻿@model ShipmentModel

@using System.Text

@{
    //page title
    ViewBag.Title = string.Format(T("Admin.Orders.Shipments.AddNew.Title").Text, Model.OrderId);
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Orders");

    var itemsFromMultipleWarehouses = Model.Items
        .Where(x => x.AllowToChooseWarehouse && x.AvailableWarehouses.Count > 0)
        .ToList();
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @string.Format(T("Admin.Orders.Shipments.AddNew.Title").Text, Model.OrderId)
            <small><i class="fa fa-arrow-circle-left"></i>@Html.ActionLink(T("Admin.Orders.Shipments.BackToOrder").Text, "Edit", new { id = Model.OrderId })</small>
        </h1>
        <div class="pull-right">
            <button type="submit" id="shipment-save" name="save" class="btn bg-blue">
                <i class="fa fa-floppy-o"></i>
                @T("Admin.Common.Save")
            </button>
            <button type="submit" id="shipment-save-continue" name="save-continue" class="btn bg-blue">
                <i class="fa fa-floppy-o"></i>
                @T("Admin.Common.SaveContinue")
            </button>
        </div>
    </div>
    @Html.ValidationSummary(false)

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            <div class="col-md-3">
                                @Html.SDFLabelFor(model => model.TrackingNumber)
                            </div>
                            <div class="col-md-9">
                                @Html.SDFEditorFor(model => model.TrackingNumber)
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-3">
                                @Html.SDFLabelFor(model => model.AdminComment)
                            </div>
                            <div class="col-md-9">
                                @Html.SDFEditorFor(model => model.AdminComment)
                            </div>
                        </div>
                    </div>
                    @if (itemsFromMultipleWarehouses.Any())
                    {
                        <script type="text/javascript">
                            $(document).ready(function() {
                                $('#shipment-save').click(function() {
                                    return validateWarehouseAvailability();
                                });
                                $('#shipment-save-continue').click(function() {
                                    return validateWarehouseAvailability();
                                });
                            });

                            function validateWarehouseAvailability() {
                                @{
                                    var sb = new StringBuilder();
                                    for (int i = 0; i <= itemsFromMultipleWarehouses.Count - 1; i++)
                                    {
                                        var item = itemsFromMultipleWarehouses[i];
                                        sb.AppendFormat("validateAvailableQuantity('{0}')", item.OrderItemId);
                                        if (i != itemsFromMultipleWarehouses.Count - 1)
                                        {
                                            sb.Append(" && ");
                                        }
                                    }
                                }
                                var valid = @(Html.Raw(sb.ToString()));
                                if (!valid) {
                                    return confirm('@T("Admin.Orders.Shipments.Products.Warehouse.QuantityNotEnough")');
                                }
                                return true;
                            }

                            function validateAvailableQuantity(orderItemId) {
                                var enteredValue = parseInt($('#qtyToAdd' + orderItemId).val(), 10);
                                if (enteredValue <= 0)
                                    return true;
                                var reservedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('reserved-qty'), 10);
                                var plannedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('planned-qty'), 10);
                                var availableToAdd = reservedValue - plannedValue;
                                return enteredValue <= availableToAdd;
                            }
                        </script>
                    }
                </div>

                <div class="panel panel-default">
                    <div class="panel-heading">
                        @T("Admin.Orders.Shipments.Products")
                    </div>
                    <div class="panel-body">
                        <table class="table table-hover table-bordered text-center">
                            <thead>
                                <tr>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.ProductName")
                                    </th>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.SKU")
                                    </th>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.Warehouse")
                                    </th>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.ItemWeight")
                                    </th>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.ItemDimensions")
                                    </th>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.QtyOrdered")
                                    </th>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.QtyShipped")
                                    </th>
                                    <th>
                                        @T("Admin.Orders.Shipments.Products.QtyToShip")
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model.Items)
                                {
                                    <tr>
                                        <td style="width: 25%;text-align: left">
                                            <em><a href="@Url.Action("Edit", "Product", new {id = item.ProductId})">@item.ProductName</a></em>
                                            @if (!String.IsNullOrEmpty(item.AttributeInfo))
                                            {
                                                <p>
                                                    @Html.Raw(item.AttributeInfo)
                                                </p>
                                            }
                                            @if (item.ShipSeparately)
                                            {
                                                <p>
                                                    <strong>@T("Admin.Orders.Shipments.Products.ShipSeparately.Warning")</strong>
                                                    @T("Admin.Orders.Shipments.Products.ShipSeparately")
                                                </p>
                                            }
                                        </td>
                                        <td style="width: 10%;">
                                            @item.Sku
                                        </td>
                                        <td style="width: 15%;">
                                            @if (item.AllowToChooseWarehouse)
                                            {
                                                if (item.AvailableWarehouses.Count > 0)
                                                {
                                                    <select name="warehouse_@(item.OrderItemId)" id="warehouse_@(item.OrderItemId)" class="form-control">
                                                        @foreach (var warehouse in item.AvailableWarehouses)
                                                        {
                                                            <option value="@warehouse.WarehouseId" data-stock-qty="@warehouse.StockQuantity" data-reserved-qty="@warehouse.ReservedQuantity" data-planned-qty="@warehouse.PlannedQuantity">
                                                                @T("Admin.Orders.Shipments.Products.Warehouse.ChooseQty", warehouse.WarehouseName, warehouse.StockQuantity, warehouse.ReservedQuantity, warehouse.PlannedQuantity)
                                                            </option>
                                                        }
                                                    </select>
                                                }
                                                else
                                                {
                                                    @T("Admin.Orders.Shipments.Products.Warehouse.NotAvailabe")
                                                }
                                            }
                                            else
                                            {
                                                //display warehouses
                                                foreach (var warehouse in item.AvailableWarehouses)
                                                {
                                                    <div>@warehouse.WarehouseName</div>
                                                }
                                            }
                                        </td>
                                        <td style="width: 10%;">
                                            @item.ItemWeight
                                        </td>
                                        <td style="width: 10%;">
                                            @item.ItemDimensions
                                        </td>
                                        <td style="width: 10%;">
                                            @item.QuantityOrdered
                                        </td>
                                        <td style="width: 10%;">
                                            @item.QuantityInAllShipments
                                        </td>
                                        <td style="width: 10%;">
                                            <input name="qtyToAdd@(item.OrderItemId)" type="text" value="@item.QuantityToAdd" id="qtyToAdd@(item.OrderItemId)" class="form-control input-sm" />
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
}